import React from 'react'
// ===================================================================== Temp
import TableModal from '#cpt/temp/TableModal'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== Declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const CheckProjectModal = $Lazy.state(()=>import('./check-project-modal'))
const ExperimentalResultModal = $Lazy.state(()=>import('./experimental-result-modal'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '只显示相同项目',	 name: 'is_same_kind',  	type:'checkbox' },
			{ label: '只显示半年',		 name: 'only_date', 		type:'checkbox' },
			{ label: '科室',			 name: 'parent_project_id',	type: 'select', loading:true, idStr:'uuid' },
			{ label: '岗位',			 name: 'project_id',		type: 'select', loading:true },
		]
	}
	
	model = {}
	query = {}
	
	
	componentDidMount(){
		this.result_model = +$fn.getCenterQuery()['result_model']
	}
	
	open = (rows, project_id) => {
		this.getSelect()
		
		const { name, parent_kind_name, patient_name  } = rows || {}
		this.modal.open()
		// 搜索参数
		// 新增岗位ID参数 20211020 耿琰
		this.query = { patient_name: name||patient_name, parent_kind_names: parent_kind_name, is_same_kind:0, only_date: 0, check_project_id:project_id }
		//
		this.fetch()
		//
		if($fn.dataSave('history-modal')){
			this?.table?.refreshCol('history-modal')
		}
	}
	
	//获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 科室
			$fn.setRow(forms, 'parent_project_id', await f.getCache({ api:'ke_shi' } ) )
			// 岗位
			$fn.setRow(forms, 'project_id', await f.getCache({ api:'gang_wei' } ) )
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this, 'ts-report-card/samenameinfo', { isIndex:true })
	
	// 表单
	cols = [
		{ title: '条码号', 		dataIndex: 'spec_code', 			...$fn.table.spec_code },
		{ title: '实验号', 		dataIndex: 'experiment_num', 		...$fn.table.exp_num },
		{ title: '科室', 		dataIndex: 'project_parent_name', 	width:100 },
		{ title: '岗位', 		dataIndex: 'project_name', 			width:100 },
		{ title: '姓名', 		dataIndex: 'patient_name', 			...$fn.table.name },
		{ title: '性别', 		dataIndex: 'sex_name', 				...$fn.table.sex },
		{ title: '年龄', 		dataIndex: 'age_first_val', 		...$fn.table.age, render: (text, {age_first_val}) => age_first_val ? age_first_val + '岁' : '--' },
		{ title: '医院', 		dataIndex: 'hosp_name', 			width:200 },
		// 查看同名历史记录弹窗新增字段  20211009 耿琰
		// 取消结果字段 2021-10-13 11:53:25 ?F10: AM? --伍智睿
		// { title: '结果', 		dataIndex: 'result', 				width:100 },
		{ title: '证件类型', 	dataIndex: 'card_type_name', 		...$fn.table.card_type },
		{ title: '证件号', 		dataIndex: 'card_no', 				...$fn.table.idcard },
		{ title: '电话', 		dataIndex: 'patient_phone', 		...$fn.table.phone },
		{ title: '报告时间', 	dataIndex: 'ts_approve_at', 		...$fn.table.time  },
		{ title: '自然项目', 	dataIndex: 'parent_kind_name', 	width:200 },
		TableControls.controls.call(this,{ width: 180 })
	]
	
	// 表格操作按钮
	table_controls = (text, rows) => [
		TableControls.click.call(this, '检测项目', ()=> this.checkProjectModal.open(rows) ),
		rows.is_pathology && TableControls.click.call(this, '实验结果', ()=> this.experimentalResultModal.open(rows) )
	]
	
	render(){
		return (
			<>
				{ TableModal.call(this,{ title:'查看同名历史记录', width:'99%', height:'80%', noSubmit:true, 
					table:{ isIndex:true, setHeader:true },
					search: {
						onSetHeader: () => {
							 this?.table?.openHeader?.(this.cols, 'history-modal')
						}
					}
				}) }
				<CheckProjectModal onRef={ ref => this.checkProjectModal = ref } />
				<ExperimentalResultModal onRef={ ref => this.experimentalResultModal = ref } />
			</>
		)
	}
}

export default Index